package com.googlecode.jascii;

import java.io.File;
import java.io.IOException;
import java.util.ArrayList;

import org.apache.commons.io.FileUtils;

/**
 * @author Kim Lee
 */
public class TableResult {
	private ArrayList trArray = new ArrayList();
	private ArrayList headerArray = new ArrayList();
	private TextStyle textStyle ;
	protected int maxwidth = 0;
	public TableResult() {
		this.textStyle = new TextStyle();
	}
	public TableResult(TextStyle textStyle) {
		this.textStyle = textStyle;
	}
	
	public TextStyle getTextStyle() {
		return textStyle;
	}
	public TableResult appendHeader(TrResult trResult) {

		if (trResult.getHeight() > 1) {
			for (int i = 1; i < trResult.getHeight() + 1; i++) {
				headerArray.add(trResult.getTrResult(i));
			}
		} else
			headerArray.add(trResult);

		return this;
	}
	public TableResult append(TrResult trResult) {
		if(maxwidth < trResult.getWidth())maxwidth = trResult.getWidth();	
		if (trResult.getHeight() > 1) {
			for (int i = 1; i < trResult.getHeight() + 1; i++) {
				
				trArray.add(trResult.getTrResult(i));
			}
		} else{
			trArray.add(trResult);
		}
		
		return this;
	}
	
	public String toString() {
		StringBuffer sb = new StringBuffer();
		for (int i = 0; i < headerArray.size(); i++) {
			TrResult td = (TrResult) headerArray.get(i);
			sb.append(td);
			sb.append("\n");//去掉\r
		}
		
		for (int i = 0; i < trArray.size(); i++) {
			TrResult td = (TrResult) trArray.get(i);
			if(td.getWidth()==0 && td.getTrType() == TrResult.TD_SCRIPT_TYPE)
				td.toString();
			else
			{
				sb.append(td);
				sb.append("\n"); //去掉\r
			}
		}
		
		return sb.toString();
	}
	
		
	
	
	
	public ArrayList getTrAsciiArray()
	{
		ArrayList list = new ArrayList(headerArray);
		
		
		list.addAll(trArray);
		return list;
	}
	public static void main(String[] args) {
		
		TableResult table = new TableResult();
		TextStyle tstyle = table.getTextStyle();
		table.append(new TrResult().append(new TdResult(tstyle.getTopLayout(tstyle.LEFT),1,TdResult.TD_ASCII_TYPE ))
				.append(new TdResult("",16,TdResult.TD_ASCII_TYPE,tstyle.getTopLayout(tstyle.COLBETWEEN) ))
				.append(new TdResult(tstyle.getTopLayout(tstyle.AROSS),1,TdResult.TD_ASCII_TYPE ))
				.append(new TdResult("",36,TdResult.TD_ASCII_TYPE,tstyle.getTopLayout(tstyle.COLBETWEEN) ))
				.append(new TdResult(tstyle.getTopLayout(tstyle.AROSS),1,TdResult.TD_ASCII_TYPE ))
				.append(new TdResult("",12,TdResult.TD_ASCII_TYPE,tstyle.getTopLayout(tstyle.COLBETWEEN) ))
				.append(new TdResult(tstyle.getTopLayout(tstyle.AROSS),1,TdResult.TD_ASCII_TYPE ))
				.append(new TdResult("",18,TdResult.TD_ASCII_TYPE,tstyle.getTopLayout(tstyle.COLBETWEEN) ))
				.append(new TdResult(tstyle.getTopLayout(tstyle.AROSS),1,TdResult.TD_ASCII_TYPE ))
				.append(new TdResult("",18,TdResult.TD_ASCII_TYPE,tstyle.getTopLayout(tstyle.COLBETWEEN) ))
				.append(new TdResult(tstyle.getTopLayout(tstyle.RIGHT),1,TdResult.TD_ASCII_TYPE ))
				);
		table.append(new TrResult()
			.append( new TdResult(tstyle.getMidLayout(TextStyle.ROWBETWEEN),1,TdResult.TD_ASCII_TYPE))
			.append(new TdResult("財產編號" , 16, TdResult.TD_STYLE_MIDDLE))
			.append( new TdResult(tstyle.getMidLayout(TextStyle.ROWBETWEEN),1,TdResult.TD_ASCII_TYPE))
			.append( new TdResult("號號號號號號號號號號號號號號號號號號號號號號號號號號號號號號號號號號號號中文輸入法測試英國專", 36 , TdResult.TD_STYLE_LEFT))
			.append( new TdResult(tstyle.getMidLayout(TextStyle.ROWBETWEEN),1,TdResult.TD_ASCII_TYPE))
			.append( new TdResult("明細編號" , 12, TdResult.TD_STYLE_MIDDLE))
			.append( new TdResult(tstyle.getMidLayout(TextStyle.ROWBETWEEN),1,TdResult.TD_ASCII_TYPE))
			.append( new TdResult("0001 - 0001" , 18, TdResult.TD_STYLE_MIDDLE))
			.append( new TdResult(tstyle.getMidLayout(TextStyle.ROWBETWEEN),1,TdResult.TD_ASCII_TYPE))
			.append( new TdResult("2009會計年度" , 18, TdResult.TD_STYLE_MIDDLE))
			.append( new TdResult(tstyle.getMidLayout(TextStyle.ROWBETWEEN),1,TdResult.TD_ASCII_TYPE))
			);
		table.append(new TrResult()
		.append( new TdResult(tstyle.getMidLayout(TextStyle.ROWBETWEEN),1,TdResult.TD_ASCII_TYPE))
		.append(new TdResult("財產編號" , 16, TdResult.TD_STYLE_MIDDLE))
		.append( new TdResult(tstyle.getMidLayout(TextStyle.ROWBETWEEN),1,TdResult.TD_ASCII_TYPE))
		.append( new TdResult("號號號號號號號號號號號號", 36 , TdResult.TD_STYLE_LEFT))
		.append( new TdResult(tstyle.getMidLayout(TextStyle.ROWBETWEEN),1,TdResult.TD_ASCII_TYPE))
		.append( new TdResult("明細編號" , 12, TdResult.TD_STYLE_MIDDLE))
		.append( new TdResult(tstyle.getMidLayout(TextStyle.ROWBETWEEN),1,TdResult.TD_ASCII_TYPE))
		.append( new TdResult("0001 - 0001" , 18, TdResult.TD_STYLE_MIDDLE))
		.append( new TdResult(tstyle.getMidLayout(TextStyle.ROWBETWEEN),1,TdResult.TD_ASCII_TYPE))
		.append( new TdResult("2009會計年度" , 18, TdResult.TD_STYLE_MIDDLE))
		.append( new TdResult(tstyle.getMidLayout(TextStyle.ROWBETWEEN),1,TdResult.TD_ASCII_TYPE))
		);
	
		table.append(new TrResult().append(new TdResult(tstyle.getBottomLayout(tstyle.LEFT),1,TdResult.TD_ASCII_TYPE ))
				.append(new TdResult("",16,TdResult.TD_ASCII_TYPE,tstyle.getBottomLayout(tstyle.COLBETWEEN) ))
				.append(new TdResult(tstyle.getBottomLayout(tstyle.AROSS),1,TdResult.TD_ASCII_TYPE ))
				.append(new TdResult("",36,TdResult.TD_ASCII_TYPE,tstyle.getBottomLayout(tstyle.COLBETWEEN) ))
				.append(new TdResult(tstyle.getBottomLayout(tstyle.AROSS),1,TdResult.TD_ASCII_TYPE ))
				.append(new TdResult("",12,TdResult.TD_ASCII_TYPE,tstyle.getBottomLayout(tstyle.COLBETWEEN) ))
				.append(new TdResult(tstyle.getBottomLayout(tstyle.AROSS),1,TdResult.TD_ASCII_TYPE ))
				.append(new TdResult("",18,TdResult.TD_ASCII_TYPE,tstyle.getBottomLayout(tstyle.COLBETWEEN) ))
				.append(new TdResult(tstyle.getBottomLayout(tstyle.AROSS),1,TdResult.TD_ASCII_TYPE ))
				.append(new TdResult("",18,TdResult.TD_ASCII_TYPE,tstyle.getBottomLayout(tstyle.COLBETWEEN) ))
				.append(new TdResult(tstyle.getBottomLayout(tstyle.RIGHT),1,TdResult.TD_ASCII_TYPE ))
				);
		
		
		
//			page.append( new TrResult().append(new TdResult("統治單位" , 12, TdResult.TD_STYLE_MIDDLE))
//			                           .append( new TdResult("HO", 6 , TdResult.TD_STYLE_MIDDLE))
//									   .append( new TdResult("財產名稱" , 12, TdResult.TD_STYLE_MIDDLE))
//									   .append( new TdResult("桌木" , 63, TdResult.TD_STYLE_LEFT))
//										);
		PageResult page = new PageResult();	
		page.append(table);
		try {
			FileUtils.writeStringToFile(new File("page.txt"), page.toString(), "BIG5");
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		
	}
	
	/**
	 * @param textStyle The textStyle to set.
	 */
	public void setTextStyle(TextStyle textStyle) {
		this.textStyle = textStyle;
	}
	public int getWidth() {
		return maxwidth;
	}
}
